package bon.idlesun.example.array2list;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import bon.idlesun.util.ArrayHelper;

public class ArrayToList {

	static void testStringArray() {
		String dayArray[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
		
		List<String> dayList = Arrays.asList(dayArray);
//		dayList.add("Sun");		// -->
//		dayList.remove(0);		// -->
		System.out.println(dayList);
		
		List<String> dayListWithSun = new ArrayList<String>(dayList);
		dayListWithSun.add("Sun");
		
		System.out.println(dayListWithSun);
	}
	
	static void testIntArray() {
		int primeNumArray[] = { 3, 5, 7, 11, 13, 17, 19, 23 };
		
		// Cannot convert from List<int[]> to List<Integer>
//		List<Integer> primeNumList = Arrays.asList(primeNumArray);
		
		List<int[]> primeNumArrayList = Arrays.asList(primeNumArray);
		System.out.println(primeNumArrayList.get(0)[0]);	// --> will print 3
		System.out.println(primeNumArrayList.get(0)[1]);	// --> will print 5
		
		List<Integer> primeListFrozen = ArrayHelper.I.asObjectList(primeNumArray);
		// --> will return immutable Object list backed by Integer array 
		
		try {
			primeListFrozen.remove(primeListFrozen.size()-1);	// --> will throw Exception
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		}
		System.out.println(primeListFrozen);

		primeListFrozen = primeListFrozen.subList(0, primeListFrozen.size()-1);
		System.out.println(primeListFrozen);
		
		List<Integer> primeList = ArrayHelper.I.toObjectList(primeNumArray);
		// --> will return immutable Object list backed by Integer array 
		System.out.println(primeList);
		
		primeList.remove(primeList.size()-1);
		primeList.add(0, 2);
		System.out.println(primeList);
		
		Integer[] primeArray = primeList.toArray(new Integer[primeList.size()]);
		for (Integer p : primeArray) {
			System.out.print(p + " ");
		}
		System.out.println("");
		
		int[] primeNumArrayFixed = ArrayHelper.I.toArray(primeList);
		for (int p : primeNumArrayFixed) {
			System.out.print(p + " ");
		}
		System.out.println("");
		
		ArrayHelper.I arrayHelper = new ArrayHelper.I();
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		testStringArray();
		testIntArray();
	}

}
